package com.wbk.lotterysystemsystemservice.controller.auth.biz;

import cn.hutool.core.io.FileUtil;
import com.wbk.lotterysystemsystemservice.service.UploadService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import org.springframework.web.multipart.MultipartFile;
import result.Result;
import tools.ImageUtil;

import java.io.File;
import java.io.IOException;

@Component
public class UploadBiz {

    @Autowired
    UploadService uploadService;

    public Result<String> uploadImag(Long userNo, MultipartFile file) throws IOException {
        if (file.isEmpty()) {
            return new Result<>(100, "文件不能为空!!!", null);
        }
        //判断file类型是否为图片
        boolean flag = ImageUtil.isImage(file.getInputStream());
        if (!flag) {
            return new Result<>(100, "图片格式不正确或已损坏!!!", null);
        }
        Result<String> result = uploadService.uploadImag(userNo, file);
        if (result.getCode() != 200) {
            return new Result<>(100, result.getMessage(), result.getData());
        }
        return new Result<>(200, result.getMessage(), result.getData());
    }

    public String uploadQrCode(Long storeUserNo, File file) throws IOException {
        if (FileUtil.isEmpty(file)) {
            return null;
        }
        //判断file类型是否为图片
        boolean flag = ImageUtil.isImage(FileUtil.getInputStream(file));
        if (!flag) {
            return null;
        }
        return uploadService.uploadQrCode(storeUserNo, file);
    }
}
